import {Injectable} from "@angular/core";
import {HttpHelper,DataCacheStrategy,PagerParam,GoodParam,Good,AuditState} from "df_ng_mall_services";
@Injectable()
export class ShopGoodService{
    private goodsUrl: string;
    private specUrl:string;
    constructor(private helper:HttpHelper,private cacheStrategy:DataCacheStrategy){
        this.goodsUrl="/rest/shop-manager/good";
        this.specUrl="/rest/shop-manager/specification";
    }

    private buildParam(pager:PagerParam,state:AuditState,goodParam?:GoodParam):string{
        if(!pager) pager=new PagerParam;
        let body = "?page=" + pager.page + "&size=" + pager.size;
        if(state) body+="&state="+state;
        if(goodParam){
            if (goodParam.category) body += "&category=" + goodParam.category;
            if(goodParam.orderby){
                body += "&orderby=" + goodParam.orderby;
            }
            if(goodParam.orderby && goodParam.asc){
                body += "&asc=" + goodParam.asc;
            }
            if(goodParam.name){
                body += "&name=" + goodParam.name;
            }
            if (goodParam.tag && goodParam.tag.length!=0) {
                goodParam.tag.forEach(t=>{
                    body+="&tag="+t;
                });
            }
            if(goodParam.shop){
                body+="&shop="+goodParam.shop;
            }
            if(goodParam.currency){
                body+="&currency="+goodParam.currency;
            }
        }
        return body;
    }

    /**
     * 获取店铺产品数量
     * @param pager 产品分页参数
     * @param state 产品审核状态
     * @param goodParam 产品查询参数
     * @returns {Promise<string>} 店铺产品数量
     */
    goodsCount(pager:PagerParam,state:AuditState,goodParam?:GoodParam):Promise<string>{
        return this.helper.getText(this.goodsUrl+this.buildParam(pager,state,goodParam));
    }

    /**
     * 获取店铺产品列表
     * @param pager 产品分页参数
     * @param state 产品审核状态
     * @param goodParam 产品查询参数
     * @returns {Promise<Good[]>}
     */
    goods(pager:PagerParam,state:AuditState,goodParam?:GoodParam):Promise<Good[]>{
        return this.helper.getJson<Good[]>(this.goodsUrl+this.buildParam(pager,state,goodParam));
    }

    /**
     * 商品上下架，商品库存修改
     * @Deprecated
     * @param id    商品的规格的ID
     * @param body  上下架{changeUsed}，库存修改{purchase}
     * @param qty   需更改的库存数量
     */
    // changeSpecification(id,body,qty?,used?):Promise<any>{
    //     let url = `${this.specUrl}/${id}`;
    //     if(qty) url+=`?qty=${qty}`;
    //     return  this.helper.putToBoolean(url,body);
    // }
    /**
     * 商品上下架，商品库存修改
     * @param id    商品的规格的ID
     * @param body  上下架{changeUsed}，库存修改{purchase}
     * @param qty   需更改的库存数量
     */
    changeSpecification(id,body,qty?):Promise<any>{
        let formParam = `spec=${id}&op=${body}`;
        // if(qty) url+=`?qty=${qty}`;
        return  this.helper.putToBoolean(this.specUrl,body,'application/x-www-form-urlencoded');
    }



    /**
     * 页面产品添加
     * @param good
     */
    addGood(good:Good):Promise<any>{
        return this.helper.postToJson(this.goodsUrl,good);
    }

    /**
     * 页面产品规则添加
     * @Deprecated
     * @param good  产品id
     * @param qty   库存
     * @param withSpecifications  规则
     */
    addGoodsSpecification(good,qty,withSpecifications):Promise<any>{
        let pass = `?good=${good}&qty=${qty}`;
        return this.helper.postToJson(this.specUrl+pass,withSpecifications);
    }



    changeStock(id,body,qty?):Promise<any>{
        let formParam = `spec=${id}&op=${body}`;
        if(qty) formParam = `spec=${id}&op=${body}&qty=${qty}`

        return  this.helper.putToBoolean(this.specUrl,formParam,'application/x-www-form-urlencoded');
    }


    /**
     * 商品修改
     * @param good
     */
    updateGood(good):Promise<any>{
        return this.helper.putToJson(this.goodsUrl,good);
    }

    /**
     * 商品规格修改
     * @param id
     * @param num
     * @param rule
     * @param spec
     */
    updateGoodsSpecifications(id,spec?,num?):Promise<any>{
        let url=`${this.specUrl}`;
        if (num){
            url =`${this.specUrl}?qty=${num}`;
        }

        return this.helper.putToJson(url,spec);
    }

    /*
    * 通过ID拉去产品信息
    * */
    good(id):Promise<any> {
        let url = this.goodsUrl + `/${id}`
        return this.helper.getJson(url);
    }

    /*
    * 添加积分
    * */
    addCritedBonus(id,currency,num):Promise<boolean>{
        let url=`/rest/credit-bonus`;
        let formParam = `spec=${id}&bonus=${num}&currency=${currency}`;
        return  this.helper.postToBoolean(url,formParam,'application/x-www-form-urlencoded');
    }

    /*删除积分*/

    deleteCredits(spec,currency):Promise<boolean>{
        let url = `/rest/credit-bonus?spec=${spec}&currency=${currency}`;
        return this.helper.remove(url);
    }
}